/*!
    This file is part of KapiSkript.

    Copyright (C) 2009 RaNaN

    KapiSkript is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Foobar is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
 */




var kapitool = {
	init: function(){
	this.transportService = Components.classes["@mozilla.org/network/socket-transport-service;1"] .getService(Components.interfaces.nsISocketTransportService);

	//window.addEventListener("DOMContentLoaded", this.main, true);	
	 window.setTimeout("kapitool.main();", 10000);


},
	showContextMenu: function(e){
document.getElementById("kapitool").hidden = !kl_isKapilandDomain(gBrowser.selectedBrowser.contentDocument.location);
},
	main: function(){
kl_eval(document,"id('contentAreaContextMenu')")[0].addEventListener("popupshowing", function(e) { kapitool.showContextMenu(e); }, false);
},
	sendData: function(outputData){

var transport = this.transportService.createTransport(null,0,"127.0.0.1",10999,null);
	var outstream = transport.openOutputStream(0,0,0);
	outstream.write(outputData,outputData.length);
	alert(outputData);

},

	synch: function(){

var doc = gBrowser.selectedBrowser.contentDocument;

if(kl_isKapilandDomain(doc.location)){

var output = "KapiSkript["+kl_v+"]\n";

// welt 1-4!!!!
try{
var menue = kl_eval(doc,"//a[contains(@href,'levels')]/..");

output += kl_eval(doc,"//font/b")[0].textContent +"|";
output += kl_evalNode(doc,menue[0],"./text()[6]")[0].textContent.match(/[a-zäöüß]+/ig)[1] + "|";
output += kl_evalNode(doc,menue[0],"./text()[3]")[0].textContent.match(/[[0-9,.]+/)[0].replace(/[.]/g,"").replace(",",".") + "|";
output += kl_evalNode(doc,menue[0],"./text()[4]")[0].textContent.match(/[[0-9,.]+/)[0].replace(/[.]/g,"").replace(",",".") + "\n";
}
catch(e){
output += "Firma|Manager|0|0\n";
}

if(doc.location.href.search("page=gebs&") > -1){

output += "Produktion";

var gebs = kl_eval(doc,"//a[contains(@href,'page=roh&')]");

for(var i=0;i<gebs.length;i++){

output += "\n" + gebs[i].href.match(/y=([0-9]+)&/)[1] + "|" + gebs[i].href.match(/x=([0-9]+)&/)[1] + "|" + gebs[i].href.match(/art=([0-9]+)&/)[1] + "|" + gebs[i].parentNode.parentNode.childNodes[1].textContent.match(/([0-9]+) m²/)[1];

}
}else if (doc.location.href.search("page=kaufs&") > -1){

output += "Verkauf";

var gebs = kl_eval(doc,"//a[contains(@href,'page=kauf&')]");

for(var i=0;i<gebs.length;i++){

output += "\n" + gebs[i].href.match(/y=([0-9]+)&/)[1] + "|" + gebs[i].href.match(/x=([0-9]+)&/)[1] + "|" + gebs[i].href.match(/art=([0-9]+)&/)[1] + "|" + gebs[i].parentNode.parentNode.cells[1].textContent.match(/([0-9]+)m²/)[1];
}

}else if (doc.location.href.search("page=forschungsbericht&") > -1){

output += "Forschungen";

var forschungen = kl_eval(doc,"//a[contains(@href,'page=forschen12&')]");

for(var i=0;i<forschungen.length;i++){

output += "\n" + forschungen[i].href.match(/p=([0-9]+)&/)[1] + "|" + forschungen[i].href.match(/q=([0-9]+)/)[1];
}



}else if (doc.location.href.search("page=lager&") > -1){

output += "Lager";

var seiten = kl_eval(doc,"//a[contains(@href,'start=') and descendant-or-self::*[contains(text(),number())] ]");
try{
var akt = doc.location.href.match(/start=([0-9]+)&/)[1];
}catch(e){
var akt = 0;
}

if(seiten.length == 0){
var produkte = 20;
}else produkte = 20 * (seiten.length / 2);

output += "\n" + produkte + "|" + akt;

var links = kl_eval(doc,"//a[contains(@href,'page=markt2&')]");

for(var i=0;i < links.length;i++){

output += "\n" + links[i].href.match(/search=([0-9]+)&/)[1] + "|" + links[i].parentNode.parentNode.childNodes[2].textContent + "|" +  links[i].parentNode.parentNode.childNodes[0].textContent.replace(/[., ]/g,"");

}

}

output += "\n[Ende]";

kapitool.sendData(output);

}

}


};
//kl_ktInit();
kapitool.init();